#include "./../inc/main.h"

void fightScene(per_p per, monster_p monsterdata, int count, int hurt)
{
    clearScreen();
    if (count != 0)
    {
        printf("\t\t\t第%d回合\t\t\t\n", count / 2);
    }
    else
    {
        printf("\t\t\t战斗开始!\t\t\t\n");
    }

    printf("====================================================\n");
    printf("\t我方\t\t\t\t敌方\t\n");
    printf("====================================================\n");
    printf("\n");
    if (count != 0)
    {
        if (count % 2 == 1)
        {
            printf(COLOR_RED "\t-%d\n" COLOR_RESET, hurt);
        }
        else
        {
            printf(COLOR_RED "\t\t\t\t\t-%d\n" COLOR_RESET, hurt);
        }
    }
    else
    {
        printf("\n");
    }
    printf("血量:\t%d\t\t\t\t%d\t\n", per->HP, monsterdata->HP);
    printf("\t英雄\t\t\t\t%s\n", monsterdata->monster_name);
    printf("\n");
    printf("\n");
    printf("\n");
}

// status 0:胜利 1:失败
int fight(per_p per, monster_Node_p currMonster)
{
    int status = 0;
    int perHP = per->HP;
    int count = 0;
    int hurt = 0; // 伤害
    monster_p monsterdata = (monster_p)malloc(sizeof(monster_t));
    memcpy(monsterdata, currMonster->data, sizeof(monster_t));
    fightScene(per, monsterdata, count, hurt);
    count++;
    cross_sleep(500);
    while (monsterdata->HP > 0 && per->HP > 0)
    {
        if (count % 2 == 0)
        {
            hurt = monsterdata->ATK - per->DEF;
            if (hurt < 0)
            {
                hurt = 0;
            }
            per->HP -= hurt;
        }
        else
        {
            hurt = per->ATK - monsterdata->DEF;
            if (hurt < 0)
            {
                hurt = 0;
            }
            monsterdata->HP -= hurt;
        }
        count++;
        fightScene(per, monsterdata, count, hurt);
        cross_sleep(500);
    }
    if (per->HP <= 0)
    {
        printf("你输了!请提高实力再来吧\n");
        per->HP = perHP;
        status = 1;
    }
    else
    {
        printf("恭喜你获得胜利!\n");
        printf("获得金币:%d\n", monsterdata->gold);
        per->gold += monsterdata->gold;
    }
    free(monsterdata);
    waitForAnyKey();
    return status;
}
